using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using RSSRadio.Model;

namespace RSSRadio
{
	public partial class FeedEpisodeDetailsForm : Form
	{
		string TmpFile;
        Subscription feed;
        EpisodeDetails episode;

		public FeedEpisodeDetailsForm()
		{
			InitializeComponent();
		}

		public FeedEpisodeDetailsForm(Subscription feed, EpisodeDetails episode)
		{
			InitializeComponent();
            this.episode = episode;
            this.feed = feed;
            this.Closing += new CancelEventHandler(FeedEpisodeDetailsForm_Closing);

			// create a temporary file with HTML describing the podcast episode
			TmpFile = Path.GetTempFileName();
			StreamWriter writer = File.AppendText(TmpFile);
			writer.Write(episode.ToMobileHTML());
			writer.Close();

            MnuDownload.Enabled = PodcastActions.canDownloadPodcast(feed, episode);

			webBrowser1.Navigate(new Uri("file://" + TmpFile));
		}

		/// <summary>
		/// Form Closing handler.  Removes temp files.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void FeedEpisodeDetailsForm_Closing(object sender, CancelEventArgs e)
		{
			// remove temporary file
			if (TmpFile != null)
				try
				{
					File.Delete(TmpFile);
				}
				catch
				{ }
		}

		private void MnuDownload_Click(object sender, EventArgs e)
		{
			PodcastActions.Download(this.feed,this.episode);
			this.Close();
		}
	}
}